<?php
/**
 * Created by PhpStorm.
 * User: 刘红波 <liuhb@cqjcdflzxsws.com>
 * Date: 2018/8/8
 * Time: 16:01
 */

namespace app\api\model;


use think\Model;

class WeNotice extends Model
{
    protected $autoWriteTimestamp = true;

    protected $name = 'we_user_notice';

    /**
     * @param $uid
     * @param $topic_id
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function vote($uid, $topic_id)
    {
        $model = new self();
        $noticeInfo = $model->where(['user_id' => $uid, 'topic_id' => $topic_id, 'type' => 0])->find();
        if (empty($noticeInfo)) {
            //新增
            $data = $model->create(['type' => 0, 'topic_id' => $topic_id, 'user_id' => $uid, 'new_vote' => 1]);
        } else {
            //更新
            $data = $noticeInfo->setInc('new_vote', '1');
        }
        if ($data) {
            return true;
        }
    }

    public static function detail($uid, $topic_id)
    {
        $model = new self();
        $noticeInfo = $model->create(['type' => 1, 'topic_id' => $topic_id, 'user_id' => $uid]);
        return $noticeInfo;
    }

    /**
     * @param $uid
     * @return array
     * @throws \think\exception\DbException
     *
     */
    public static function voteList($uid)
    {
        $model = new self();
        $data_list = $model->alias('n')
            ->join('__WE_TOPIC__ t', 'n.topic_id = t.id')
            ->where(['n.user_id' => $uid,'n.type'=>0,'t.status'=>1,'t.is_delete'=>0])
            ->field('t.id,t.likes,n.new_vote,t.content,n.create_time,n.update_time')
            ->order('n.update_time')
            ->paginate(config('sd_config.list_len'));
        return empty($data_list) ? [] : $data_list->toArray();
    }

    /**
     * @param $uid
     * @return array|\think\Paginator
     * @throws \think\exception\DbException
     */
    public static function detailList($uid)
    {
        $model = new self();

        $topic_id_arr = $model->where(['type' => 1, 'user_id' => $uid, 'is_delete' => 0])->field('topic_id')->group('topic_id')->orderRaw('count(topic_id) desc')->paginate(config('sd_config.list_len'))->toArray();
        if (empty($topic_id_arr['data'])) {
            return [];
        }
        //话题ID
        $topic_ids = array_column($topic_id_arr['data'], 'topic_id');
        $topic_model = new WeTopic();
        $field = 't.id,t.content,td.content as detail,td.create_time as td_time,u.nick_name,u.we_nick_name,u.reset_name,t.create_time as time';
        //话题评论
        $data_list = $topic_model->alias('t')
            ->join('__WE_TOPIC_DETAIL__ td', 't.id = td.topic_id and t.status = 1 and t.is_delete = 0 and td.is_delete = 0')
            ->join('__WE_USERS__ u', 'td.user_id = u.id and u.status =1 and u.is_delete = 0')
            ->field($field)
            ->where(['t.id' => ['in', $topic_ids]])
            ->order('t.create_time desc')
            ->select();
        if (empty($data_list)) {
            return [];
        }
        $data = collection($data_list)->toArray();
        $arr = [];
        foreach ($topic_ids as $id) {
            $temp = [];
            foreach ($data as $datum) {
                if (isset($datum['td_time'])){
                    $datum['td_time'] = date('Y-m-d H:i:s',$datum['td_time']);
                }
                if ($id == $datum['id']) {
                    $temp[] = $datum;
                }
            }
            $arr[] = $temp;
        }
        $temp_arr = [];
        foreach ($arr as $item) {
            $temp = [];
            foreach ($item as $value) {
                $temp['id'] = $value['id'];
                $temp['content'] = $value['content'];
                $temp['create_time'] = $value['time'];
                $temp['son'][] = ['nick_name' => $value['nick_name'],'we_nick_name'=>$value['we_nick_name'],'reset_name'=>$value['reset_name'], 'detail' => $value['detail'], 'create_time' => $value['td_time']];
            }
            if (isset($temp['son'])){
                $son_time_arr = array_column($temp['son'],'create_time');
                array_multisort($son_time_arr,SORT_DESC,$temp['son']);
            }
            $temp_arr[] = $temp;
        }

        $topic_id_arr['data'] = $temp_arr;
        return $topic_id_arr;
    }

    public function getUpdateTimeAttr($value){
        return date('Y-m-d H:i:s',$value);
    }


    public function getCreateTimeAttr($value){
        return date('Y-m-d H:i:s',$value);
    }
}